package top.mccat.web.controller.employee;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mccat.common.annotation.Log;
import top.mccat.common.core.controller.BaseController;
import top.mccat.common.core.domain.AjaxResult;
import top.mccat.common.core.domain.entity.SysUser;
import top.mccat.common.enums.BusinessType;
import top.mccat.common.core.page.TableDataInfo;
import top.mccat.common.utils.poi.ExcelUtil;
import top.mccat.system.domain.EmployeeVacation;
import top.mccat.system.entity.PunchEntity;
import top.mccat.system.entity.VacationEntity;
import top.mccat.system.service.IEmployeeVacationService;
import top.mccat.system.service.ISysUserService;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-30
 */
@RestController
@RequestMapping("/system/vacation")
public class EmployeeVacationController extends BaseController
{
    @Autowired
    private IEmployeeVacationService employeeVacationService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询【员工请假审批】列表
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmployeeVacation employeeVacation)
    {
        startPage();
        List<EmployeeVacation> list = employeeVacationService.selectEmployeeVacationList(employeeVacation);
        List<VacationEntity> vacationEntities = new ArrayList<>();
        for(EmployeeVacation vacation : list){
            SysUser sysUser = sysUserService.selectUserById(vacation.getUserId());
            VacationEntity vacationEntity = new VacationEntity(vacation,sysUser);
            vacationEntities.add(vacationEntity);
        }
        return getDataTable(vacationEntities);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:export')")
    @Log(title = "【员工请假审批】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmployeeVacation employeeVacation)
    {
        List<EmployeeVacation> list = employeeVacationService.selectEmployeeVacationList(employeeVacation);
        ExcelUtil<EmployeeVacation> util = new ExcelUtil<EmployeeVacation>(EmployeeVacation.class);
        util.exportExcel(response, list, "【员工请假审批】数据");
    }

    /**
     * 获取【员工请假审批】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(employeeVacationService.selectEmployeeVacationById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:add')")
    @Log(title = "【员工请假审批】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmployeeVacation employeeVacation)
    {
        return toAjax(employeeVacationService.insertEmployeeVacation(employeeVacation));
    }

    /**
     * 修改【员工请假审批】
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:edit')")
    @Log(title = "【员工请假审批】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmployeeVacation employeeVacation)
    {
        //如果参数不为0（未审核）表示已经进行审批
        if(!"0".equals(employeeVacation.getStatus())){
            employeeVacation.setDisplay(1);
        }
        return toAjax(employeeVacationService.updateEmployeeVacation(employeeVacation));
    }

    /**
     * 删除【员工请假审批】
     */
    @PreAuthorize("@ss.hasPermi('system:vacation:remove')")
    @Log(title = "【员工请假审批】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(employeeVacationService.deleteEmployeeVacationByIds(ids));
    }
}
